﻿using System;
using System.Linq;
using System.Collections.Generic;
using NUnit.Framework;

namespace NMA.Infrastructure.NHibernate.Tests.Repository
{
    using NMA.Domain.Model;
    using NMA.Infrastructure.NHibernate.DomainObject;

    [TestFixture]
    public class PollRepositoryTest : TestingBase
    {
        [Test]
        public void Count_all_poll_in_database()
        {
            int result = -1;

            if (PollRepository != null)
            {
                result = PollRepository.CountAllPoll();

                Assert.IsTrue(result >= 0, "contain 2 item in database");
            }
        }

        [Test]
        public void Can_get_by_id_from_database()
        {
            IPoll entity = null;

            if (PollRepository != null)
            {
                var obj = PollList.FirstOrDefault<Poll>();

                entity = PollRepository.GetById(obj.Id);

                Assert.IsNotNull(entity, "Poll is not null");
                Assert.IsTrue(entity.Id == obj.Id);
                Assert.IsTrue(entity.Name == obj.Name);
                Assert.IsTrue(entity.CreatedDate.ToShortDateString() == obj.CreatedDate.ToShortDateString());
            }
        }

        [Test]
        public void Can_get_by_poll_in_database()
        {
            IEnumerable<IPoll> result = null;

            if (PollRepository != null)
            {
                result = PollRepository.GetBy(x => x.Name == "poll name 1");

                Assert.IsNotNull(result);

                var entity = result.FirstOrDefault<IPoll>();

                Assert.IsNotNull(entity);
                Assert.IsTrue(entity.Name == "poll name 1");
                Assert.IsTrue(entity.CreatedDate.ToShortDateString() == DateTime.Now.ToShortDateString());
            }
        }

        [Test]
        public void Can_update_poll_in_database()
        {
            if (PollRepository != null)
            {
                var entity = PollList.FirstOrDefault<Poll>();

                var dbObj = PollRepository.GetById(entity.Id);

                Assert.IsNotNull(dbObj);

                dbObj.Name = "update name";
                dbObj.UpdatedDate = DateTime.Now.AddDays(1);

                // update into database
                PollRepository.Update(dbObj);

                // get out for assertion
                var updateObj = PollRepository.GetById(dbObj.Id);

                Assert.IsNotNull(updateObj);
                Assert.IsTrue(updateObj.Id == dbObj.Id);
                Assert.IsTrue(updateObj.Name == "update name");
                Assert.IsTrue(updateObj.UpdatedDate.ToShortDateString() == DateTime.Now.AddDays(1).ToShortDateString());
            }
        }

        [Test]
        public void Get_all_poll_from_database_with_paging()
        {
            if (PollRepository != null)
            {
                var result = PollRepository.All(1, 10);

                Assert.IsTrue(result.TotalCount >= 2, "Total count");
                Assert.IsTrue(result.PageIndex == 1, "Page index");
                Assert.IsTrue(result.PageSize == 10, "Page size");
            }
        }
    }
}